package com.yupi.springbootinit.service.impl;

import java.util.Date;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.model.dto.userInterfaceInfo.UserInterfaceInfoQueryRequest;
import com.yupi.springbootinit.model.entity.InterfaceInfo;
import com.yupi.springbootinit.model.entity.User;
import com.yupi.springbootinit.model.entity.UserInterfaceInfo;
import com.yupi.springbootinit.service.UserInterfaceInfoService;
import com.yupi.springbootinit.mapper.UserInterfaceInfoMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author phxz
 * @description 针对表【user_interface_info(用户—接口调用信息)】的数据库操作Service实现
 * @createDate 2024-07-11 21:38:57
 */
@Service
public class UserInterfaceInfoServiceImpl extends ServiceImpl<UserInterfaceInfoMapper, UserInterfaceInfo>
        implements UserInterfaceInfoService {

    @Resource
    private UserInterfaceInfoMapper userInterfaceInfoMapper;
    @Override
    public void validUserInterfaceInfo(UserInterfaceInfo userInterfaceInfo, boolean add) {
        if (userInterfaceInfo == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long id = userInterfaceInfo.getId();
        Long interfaceId = userInterfaceInfo.getInterfaceId();
        Long userId = userInterfaceInfo.getUserId();
        if (id < 0 || interfaceId < 0 || userId < 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
    }

    @Override
    public QueryWrapper<UserInterfaceInfo> getQueryWrapper(UserInterfaceInfoQueryRequest userInterfaceInfoQueryRequest) {


        QueryWrapper<UserInterfaceInfo> queryWrapper = new QueryWrapper<>();
        if (userInterfaceInfoQueryRequest == null) {
            return queryWrapper;
        }
        // todo 设置queryWrapper
//        Long totalCnt = userInterfaceInfoQueryRequest.getTotalCnt();
//        Long leftCnt = userInterfaceInfoQueryRequest.getLeftCnt();
//        Integer status = userInterfaceInfoQueryRequest.getStatus();
//        Date createTime = userInterfaceInfoQueryRequest.getCreateTime();
//        Date updateTime = userInterfaceInfoQueryRequest.getUpdateTime();
        return queryWrapper;
    }
    public void invokeCnt(int interfaceInfoId,int userId){
        // 判断
        if(interfaceInfoId <= 0 || userId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UpdateWrapper<UserInterfaceInfo> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("interface_id",interfaceInfoId).eq("user_id",userId);
        updateWrapper.setSql("left_cnt = left_cnt - 1, total_cnt = total_cnt + 1, update_time = now()");
        this.update(updateWrapper);
    }
}




